import 'package:flutter/material.dart';
import 'package:wheel_of_fortune/model/rotary_table_model.dart';
import 'package:wheel_of_fortune/provider/product_provider.dart';
import 'package:wheel_of_fortune/utils/utils.dart';
import 'package:wheel_of_fortune/widgets/message_alerts.dart';
import 'package:wheel_of_fortune/widgets/provider_widget.dart';

import 'widgets/collect_money.dart';

class MoneyWinPage extends StatefulWidget {
  final PrizeInfos prize;
  final String lotteryId;
  MoneyWinPage(this.prize, this.lotteryId, {Key key}) : super(key: key);

  @override
  _MoneyWinPageState createState() => _MoneyWinPageState();
}

class _MoneyWinPageState extends State<MoneyWinPage> {
  final count = 500;
  _showVerified() {
    showModalBottomSheet(
      context: context,
      enableDrag: false,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      builder: (c) {
        return AlertDialog(
          titlePadding: EdgeInsets.zero,
          title: MessageAlerts(
            type: 1,
            content:
                'You are not yet verified user,\nplease upgrade your account to\nEstablished to collect your gift.',
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final width = Utils.screenWidth;
    var left = (Utils.screenWidth - 274) / 2;
    return ProviderWidget<ProductProvider>(
      model: ProductProvider(),
      builder: (context, provider, child) {
        return WillPopScope(
          onWillPop: () async {
            final back = await showModalBottomSheet(
              context: context,
              // enableDrag: false,
              isScrollControlled: true,
              backgroundColor: Colors.transparent,
              builder: (c) {
                return AlertDialog(
                  titlePadding: EdgeInsets.zero,
                  title: MessageAlerts(
                    type: 0,
                    content: 'Are you sure to give up your gift?',
                  ),
                );
              },
            );
            if (back) {
              provider.giveUpPrize(widget.lotteryId);
            }
            return back;
          },
          child: Scaffold(
            appBar: AppBar(
              title: Text(
                'Wheel of Fortune',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 15,
                  fontWeight: FontWeight.w400,
                ),
              ),
              centerTitle: true,
            ),
            body: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.only(left: 14, top: 20, right: 14),
                    height: (width - 28) * (297 / 348),
                    child: InkWell(
                      onTap: null,
                      child: Image.asset(
                        'assets/images/money.png',
                      ),
                    ),
                  ),
                  Padding(
                      padding: EdgeInsets.fromLTRB(left, 22, left, 0),
                      child: InkWell(
                        onTap: null,
                        child: Text(
                          "Congratulations!\nYou've won a free gift!",
                          style: TextStyle(
                            color: Color(0xFF041D57),
                            fontSize: 24,
                            fontFamily: 'Avenir Next LT Pro',
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      )),
                  Padding(
                    padding: EdgeInsets.fromLTRB(left, 20, left, 20),
                    child: InkWell(
                      onTap: null,
                      child: Text(
                        "You won ${widget.prize.amount.toInt()} Pesos!",
                        style: TextStyle(
                          color: Color(0xFF041D57),
                          fontSize: 16,
                          fontFamily: 'Avenir Next LT Pro',
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.fromLTRB((width - 20 * 2 - 184) / 2, 70,
                        (width - 20 * 2 - 184) / 2, 30),
                    child: GestureDetector(
                      onTap: () async {
                        final re = await provider.collectPrize(widget.lotteryId);
                        if (re == 'success') {
                          await showModalBottomSheet(
                            context: context,
                            enableDrag: false,
                            isScrollControlled: true,
                            backgroundColor: Colors.transparent,
                            builder: (c) {
                              return AlertDialog(
                                shape: RoundedRectangleBorder(
                                  side: BorderSide.none,
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(13)),
                                ),
                                title: CollectMoney(count: widget.prize.amount.toInt(),),
                              );
                            },
                          );
                          Navigator.of(context).pop();
                        } else if (re['status'] == 2003) {
                          _showVerified();
                        }
                      },
                      child: Container(
                        height: 34,
                        margin: EdgeInsets.only(top: 20, bottom: 40),
                        decoration: BoxDecoration(
                          color: Color(0xFFFFB32C),
                          borderRadius: BorderRadius.circular(17),
                        ),
                        child: Center(
                          child: Text(
                            'Collect GIft',
                            style: TextStyle(
                                color: Color(0xFFFFFFFF),
                                fontSize: 14,
                                fontFamily: 'Avenir Next LT Pro',
                                fontWeight: FontWeight.bold),
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
